﻿/*
   Author: Tomer Paz
   games.com
   2007
*/

/* some basic dialogs for Game Controller
   the idea is to have few basic structure that can be used
   to create flexible dialogs. like building blocks and/or Templates
   implemented as functions that return the desired GUI objects
*/
/* RadioMsgBox - creates a simple message Box with text label, text box and configurable # of radio buttons + Apply button
   extends the BasicDialog (BasicMsgBox) by composition */

package monopoly.GameController.Dialogs.Templates {
	
	import fl.controls.Button;
	import fl.controls.RadioButton;
	import fl.controls.RadioButtonGroup;
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	import monopoly.GameController.Dialogs.Templates.BasicDialog;
	import monopoly.Localization.*;
	
	
	public class RadiosDialog extends Sprite
		{
		/* Properties */
		private var _BasicDialog:BasicDialog;
		private var _rbg:RadioButtonGroup;
		private var _rbLabel:String;
		private var _DoneButton:Button;
		
		/* help variables */
		private var padding:uint = 10;
        private var verticalSpacing:uint = 20;
        private var posX:Number;
        private var posY:Number = 0;

		
		public function RadiosDialog(LabelText:String, MsgText:String, RadioOptions:Array)
			{
			_BasicDialog = new BasicDialog(LabelText, MsgText, null, null);
			with (this)
				{
				x = 0;
				y = 0;
				}
			addChild(_BasicDialog);
			var lastObjectInPane:DisplayObject = _BasicDialog.getChildAt(_BasicDialog.numChildren-1);
			posY = lastObjectInPane.y + lastObjectInPane.height;
			posX = 0;
			
			_rbg = new RadioButtonGroup("RadiosDialogGroup");
			setupRadioGroup(RadioOptions);
			setupDoneButton();
			}
			
		/* handle Radio options */	
		
		private function setupRadioGroup(RadioOptions:Array):void
			{
			for (var i:int = 0; i < RadioOptions.length; i++)
				{
				createRadioButton(String(RadioOptions[i]), _rbg, posX);
				}
			posX += 80;
			//currHeight = 0;

			_rbg.addEventListener(Event.CHANGE, UpdateSelection);
			}
			
		private function createRadioButton(rbLabel:String, rbg:RadioButtonGroup, posX:uint):void
			{
			var rb:RadioButton = new RadioButton();
			rb.group = rbg;
			rb.label = rbLabel;
			_BasicDialog.addChild(rb);

			rb.move(posX, posY + padding)	// + currHeight);
			rb.width = 150;
			
			posY += verticalSpacing;		//currHeight += verticalSpacing;
			}
		
		private function UpdateSelection(e:Event):void
			{
			var rbg:RadioButtonGroup = e.target as RadioButtonGroup;
			var rb:RadioButton = rbg.selection;
			
			/* update selected option according to index of selected rbutton */
			
			_BasicDialog.ChosenOption = rbg.getRadioButtonIndex(rb);
			_rbLabel = rb.label;
			}

		private function setupDoneButton():void
			{
			var lang:ILocalLanguage = LocalLanguage.getInstance();
			_DoneButton = new Button();
			_BasicDialog.addChild(_DoneButton);
			
	        _DoneButton.move(posX, posY + padding);		// + currHeight);
			_DoneButton.setSize(55, 25);
	        _DoneButton.label = lang.DlgRadioDone;
			_DoneButton.emphasized = true;
			_DoneButton.width = 55;
	        _DoneButton.addEventListener(MouseEvent.CLICK, buttonClick);
			}
		
		private function buttonClick(e:MouseEvent):void 
			{
			/* check client has chosen something */
			
			if (BasicDialog.NO_CHOICE == this.ChosenOption)
				return;
			
			/* inform Game Controller about the decision (up to controller to remove this sprite) */
			
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
            }

		/* getter (for Game Controller once it recieves the change event) */
		
		public function get ChosenOption():int
			{
			return _BasicDialog.ChosenOption;
			}
		public function get ChosenName():String
			{
			return _rbLabel;
			}
			
		/* to be called by the Game Controller once decision made (event sent from buttonClick) */
		
		public function remove( ):void
			{
			removeChild(_BasicDialog);
			_BasicDialog.removeChild(_DoneButton);
			_BasicDialog.remove( );
			
			for (var i:int = 0; i < _rbg.numRadioButtons; i++)
				{
				_BasicDialog.removeChild(_rbg.getRadioButtonAt(i));
				_rbg.removeRadioButton(_rbg.getRadioButtonAt(i));
				}
			}
		}
}